package com.flute.tools.util;

import java.util.*;

public class SortUtil {

	public static LinkedHashMap<Object, Object> getTopsByValue(Map<?, ?> dic,
			int top) {
		Object[] objs = dic.values().toArray();
		Arrays.sort(objs);
		HashSet<Object> addedSet = new HashSet<Object>();
		LinkedHashMap<Object, Object> map = new LinkedHashMap<Object, Object>();
		for (int i = 0; i < Math.min(objs.length, top); i++) {
			for (Object key : dic.keySet()) {
				if (dic.get(key).equals(objs[objs.length - i - 1])
						&& !addedSet.contains(key)) {
					map.put(key, dic.get(key));
					addedSet.add(key);
				}
			}
		}

		return map;
	}
}
